package com.unknowpupil.design.prototype;

import java.util.Date;

/**
 * @Description: <p>
 * 模拟克隆 和创建对象的过程比对。
 *
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/24 21:32
 * @Classname: Client
 * @Version: 1.0
 */
public class Client3 {
    public static void testNew(int size) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            Laptop laptop = new Laptop();
        }
        long end = System.currentTimeMillis();
        System.out.println("new 方式创建耗时：" + (end - start));
    }

    public static void testCopy(int size) throws Exception {
        long start = System.currentTimeMillis();
        Laptop laptop = new Laptop();
        for (int i = 0; i < size; i++) {
            Laptop clone = (Laptop) laptop.clone();
        }
        long end = System.currentTimeMillis();
        System.out.println("copy 方式创建耗时：" + (end - start));
    }


    public static void main(String[] args) throws Exception {
        // copy 方式创建耗时：12
        testCopy(1000);

        //new 方式创建耗时：10596
        testNew(1000);


    }
}

/**
 * 笔记本电脑
 */
class Laptop implements Cloneable {
    public Laptop() {
        //模拟创建一个对象需要十毫米
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}